package com.zz.controller;

import java.io.IOException;
import java.nio.charset.Charset;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.catalina.util.URLEncoder;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.json.JSONArray;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.zz.entity.DeliverylistExcel;
import com.zz.entity.PoExcel;
import com.zz.service.EntryService;
import com.zz.service.PoService;

@RestController
@RequestMapping("po")
public class PoController {
	@Resource
	PoService poService;
	
	List<PoExcel> polist=new ArrayList<PoExcel>();
	//导出采购excel表
	@RequestMapping("Excelpolist")
	public List<PoExcel> getexcel(@RequestParam(value="idliet2[]") String[] idliet2){
		polist.clear();
		for (int i = 0; i < idliet2.length; i++) {
		    System.out.println("++++1++++"+idliet2[i]);
		    String poid =idliet2[i].substring(8);
		    System.out.println(poid);
		    polist.add(poService.showPoByPoid(poid));
		}
		for (int i = 0; i < polist.size();i++) {
		    System.out.println("++++2++++"+polist.get(i).getId());
		   
		}
		return polist;
	
	}
	@RequestMapping("Excelpolist2")
	public void getexcel2(HttpServletRequest request,HttpServletResponse response){
		Date d=new Date();
		SimpleDateFormat s=new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒");
		String fileName = ""+s.format(d)+"采购单.xlsx";//设置要导出的文件的名字
		//新增数据行，并且设置单元格数据
		
		XSSFWorkbook wk = new XSSFWorkbook();
		XSSFSheet sheet = wk.createSheet("采购单");
		
		
		
		// 表头样式
		XSSFFont xssfFont = wk.createFont();
		// 加粗
		xssfFont.setBold(true);
		// 字体名称
		xssfFont.setFontName("宋体");
		// 字体大小
		xssfFont.setFontHeight(12);
		XSSFCellStyle headStyle = wk.createCellStyle();
		// 设置字体css
		headStyle.setFont(xssfFont);
		// 竖向居中
		headStyle.setVerticalAlignment(VerticalAlignment.CENTER);
		// 横向居中
		headStyle.setAlignment(HorizontalAlignment.CENTER);
		// 边框
		//headStyle.setBorderBottom(BorderStyle.THIN);
		headStyle.setBorderLeft(BorderStyle.THIN);
		headStyle.setBorderRight(BorderStyle.THIN);
		headStyle.setBorderTop(BorderStyle.THIN);
		String[] headers = { "采购单单号", "商品编号", "商品名称", "采购数量","采购单创建时间","供应商","状态","入库单号","退货单号"};
		sheet.setDefaultColumnWidth(4);
		XSSFRow row1 = sheet.createRow(0);
		for (int i=0;i<headers.length;i++) {
			sheet.setColumnWidth(i, 25*256);
			XSSFCell row1Cell1 = row1.createCell(i);
			XSSFRichTextString text = new XSSFRichTextString(headers[i]);
			row1Cell1.setCellValue(text);
			row1Cell1.setCellStyle(headStyle);
		}
		
		// 内容字体样式
		XSSFFont contFont = wk.createFont();
		// 加粗
		contFont.setBold(false);
		// 字体名称
		contFont.setFontName("宋体");
		// 字体大小
		contFont.setFontHeight(11);
		// 内容样式
		XSSFCellStyle contentStyle = wk.createCellStyle();
		// 设置字体css
		contentStyle.setFont(contFont);
		// 竖向居中
		contentStyle.setVerticalAlignment(VerticalAlignment.CENTER);
		// 横向居中
		//contentStyle.setAlignment(HorizontalAlignment.CENTER);
		// 边框
		contentStyle.setBorderBottom(BorderStyle.THIN);
		contentStyle.setBorderLeft(BorderStyle.THIN);
		contentStyle.setBorderRight(BorderStyle.THIN);
		contentStyle.setBorderTop(BorderStyle.THIN);
		// 自动换行
		contentStyle.setWrapText(true);
		
		
		int rowNum = 1;
		//在表中存放查询到的数据放入对应的列
		//在表中存放查询到的数据放入对应的列
		for (PoExcel poExcellist:polist) {
			XSSFRow row2 = sheet.createRow(rowNum);
			
			row2.createCell(0).setCellValue(poExcellist.getId());
			row2.createCell(1).setCellValue(poExcellist.getProductId());
			row2.createCell(2).setCellValue(poExcellist.getProductname());
			SimpleDateFormat time = new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒");  
			row2.createCell(3).setCellValue(poExcellist.getPoNum());
			row2.createCell(4).setCellValue(time.format(poExcellist.getPoTime()));
			row2.createCell(5).setCellValue(poExcellist.getGysname());
			row2.createCell(6).setCellValue(poExcellist.getStatus());
			row2.createCell(7).setCellValue(poExcellist.getEntryId());
			row2.createCell(8).setCellValue(poExcellist.getReturnsId());
			rowNum++;
		}
		
		Charset charset = Charset.forName("UTF-8");
		
		response.setContentType("application/octet-stream;charset=utf-8");
		response.setHeader("Content-Disposition","attachment;filename="+ URLEncoder.DEFAULT.encode(fileName, charset));
		try {
			response.flushBuffer();
			wk.write(response.getOutputStream());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}
}
